public class Solution1971 {

    static class Bing{
        int[] b;
        public Bing(int n){
            b=new int[n];
            for (int i=0;i<n;i++){
                b[i]=i;
            }
        }

        public void union(int x,int y){
            int xx=find(x),yy=find(y);
            if (xx!=yy){
                b[xx]=yy;
            }
        }

        public int find(int x){
            return b[x]==x?x:(b[x]=find(b[x]));
        }
    }


    public boolean validPath(int n, int[][] edges, int source, int destination) {
        Bing bing=new Bing(n);
        for (int[] edge : edges) {
            bing.union(edge[0],edge[1]);
        }
        return bing.find(source)==bing.find(destination);
    }

    public static void main(String[] args) {
        System.out.println(new Solution1971().validPath(3,new int[][]{{0,1},{1,2},{2,0}},0,2));
    }
}
